package de.homer.server.controller;

import java.security.Principal;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

/**
 * A service checking the login data.
 * 
 * @author André Stannek
 * 
 */
@Controller
public class LoginController {

	/**
	 * Request handler for welcome.jsp.
	 * 
	 * @param model
	 *            the model for jsp
	 * @param principal
	 *            the security information about this request
	 * @return redirect to welcome page
	 */
	@RequestMapping(value = "/welcome", method = RequestMethod.GET)
	public final String welcome(final ModelMap model, final Principal principal) {

		model.addAttribute("username", principal.getName());
		return "welcome";
	}

	/**
	 * Request handler for failed login.
	 * 
	 * @param model
	 *            the model for jsp
	 * @return redirect to page that shows login error
	 */
	@RequestMapping(value = "/loginfailed", method = RequestMethod.GET)
	public final String loginerror(final ModelMap model) {
		model.addAttribute("error", "true");
		return "login";
	}

	/**
	 * @param model
	 *            the model for jsp
	 * @return redirect to page that shows after logout
	 */
	@RequestMapping(value = "/logout", method = RequestMethod.GET)
	public final String logout(final ModelMap model) {
		return "login";
	}

	/**
	 * Redirects to login page.
	 * 
	 * @param model
	 *            the model for jsp
	 * @return redirect to login page
	 */
	@RequestMapping(value = "/login", method = RequestMethod.GET)
	public final String login(final ModelMap model) {
		return "login";
	}
}
